/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-01-12 09:35:49
 * @LastEditTime : 2023-06-29 13:23:04
 * @LastEditors  : NaN 353362604@qq.com
 * @FilePath     : \AdminStration\src\Common\Login\index.jsx
 * @Description  :
 *
 */
import { project } from '../Data'
import { lazy, Suspense, useEffect } from 'react'
import { useTitle, useFavicon, useBoolean, useEventEmitter } from 'ahooks'
import { LoginSignUpGetUserInfo } from './LoginFunc'
import Loading from '../Currency/Loading'
const { client_id, login, title, imageUrl, uuid, smallTitle } = project[window.location.hostname]

const Login = () => {
    useTitle(title)
    useFavicon(imageUrl + 'Ico.ico')
    const [codeState, { toggle }] = useBoolean(false)
    const [LoginState, { setTrue, setFalse }] = useBoolean(false)
    const LoadingEvent = useEventEmitter()
    LoadingEvent.useSubscription(setFalse)
    useEffect(() => {
        // console.clear()
        sessionStorage.clear()
        localStorage.clear()
        if (window.location.search && window.location.search.indexOf('?token=Bearer%20') !== -1) {
            const [token, loginType] = window.location.search.split('&')
            const [, tokenArray] = token.split('?token=')
            const [token_type, access_token] = tokenArray.split('%20')
            let login_type = ''
            if (loginType) login_type = loginType.split('loginType=')[1]
            LoginSignUpGetUserInfo({ token_type, access_token, login_type }, 2, LoadingEvent)
            setTrue()
        } else {
            setFalse()
        }
    }, [LoadingEvent, setFalse, setTrue])

    return (
        <div>
            <Suspense fallback={<Loading />}>
                {(() => {
                    const Template = lazy(() => import(`./LoginStyle/${login || client_id}`))
                    return LoginState ? (
                        <Loading />
                    ) : (
                        <Template
                            imageUrl={imageUrl}
                            title={title}
                            smallTitle={smallTitle}
                            codeState={codeState}
                            toggle={toggle}
                            uuid={uuid}
                        />
                    )
                })()}
            </Suspense>
        </div>
    )
}
export default Login
